﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _7DaysToDieTools.Class
{
    /// <summary>
    /// 备份与还原操作类
    /// </summary>
    public static class BackAction
    {
        /// <summary>
        /// 创建备份目录
        /// </summary>
        /// <returns></returns>
        public static void CheckBackDir()
        {
            if (!Directory.Exists(Sinfo.BackPath))
            {
                Directory.CreateDirectory(Sinfo.BackPath);
            }
        }

        /// <summary>
        /// 检查是否有备份
        /// </summary>
        /// <returns></returns>
        public static bool CheckHaveBack()
        {
            if (!Directory.Exists(Sinfo.BackPath))
            {
                return false;
            }
            if (Directory.GetFiles(Sinfo.BackPath).Length <= 0)
            {
                return false;
            }

            return true;
        }

        /// <summary>
        /// 备份所有工具使用的文件
        /// </summary>
        /// <returns></returns>
        public static void BackAll()
        {
            CheckBackDir();
            //Localization.txt与Localization - Quest.txt
            if (File.Exists(Sinfo.BackPath + Sinfo.File.Localization))
            {
                File.Delete(Sinfo.BackPath + Sinfo.File.Localization);
            }
            File.Copy(Sinfo.Path.Lang + Sinfo.File.Localization, Sinfo.BackPath + Sinfo.File.Localization);

            if (File.Exists(Sinfo.BackPath + Sinfo.File.LocalizationQuest))
            {
                File.Delete(Sinfo.BackPath + Sinfo.File.LocalizationQuest);
            }
            File.Copy(Sinfo.Path.Lang + Sinfo.File.LocalizationQuest, Sinfo.BackPath + Sinfo.File.LocalizationQuest);

            //Assembly-CSharp.dll
            if (File.Exists(Sinfo.BackPath + Sinfo.File.GameDllAssembly))
            {
                File.Delete(Sinfo.BackPath + Sinfo.File.GameDllAssembly);
            }
            File.Copy(Sinfo.Path.Dll + Sinfo.File.GameDllAssembly, Sinfo.BackPath + Sinfo.File.GameDllAssembly);

            //xui.xml
            if (File.Exists(Sinfo.BackPath + Sinfo.File.Xui))
            {
                File.Delete(Sinfo.BackPath + Sinfo.File.Xui);
            }
            File.Copy(Sinfo.Path.Config + Sinfo.File.Xui, Sinfo.BackPath + Sinfo.File.Xui);

            //sytles.xml
            if (File.Exists(Sinfo.BackPath + Sinfo.File.XuiStyle))
            {
                File.Delete(Sinfo.BackPath + Sinfo.File.XuiStyle);
            }
            File.Copy(Sinfo.Path.XUi + Sinfo.File.XuiStyle, Sinfo.BackPath + Sinfo.File.XuiStyle);

            //windows.xml
            if (File.Exists(Sinfo.BackPath + Sinfo.File.XuiWindows))
            {
                File.Delete(Sinfo.BackPath + Sinfo.File.XuiWindows);
            }
            File.Copy(Sinfo.Path.XUi + Sinfo.File.XuiWindows, Sinfo.BackPath + Sinfo.File.XuiWindows);
                
            //items.xml
            if (File.Exists(Sinfo.BackPath + Sinfo.File.Items))
            {
                File.Delete(Sinfo.BackPath + Sinfo.File.Items);
            }
            File.Copy(Sinfo.Path.Config + Sinfo.File.Items, Sinfo.BackPath + Sinfo.File.Items);

            //blocks.xml
            if (File.Exists(Sinfo.BackPath + Sinfo.File.Blocks))
            {
                File.Delete(Sinfo.BackPath + Sinfo.File.Blocks);
            }
            File.Copy(Sinfo.Path.Config + Sinfo.File.Blocks, Sinfo.BackPath + Sinfo.File.Blocks);

            //rwgmixer.xml
            if (File.Exists(Sinfo.BackPath + Sinfo.File.RWG))
            {
                File.Delete(Sinfo.BackPath + Sinfo.File.RWG);
            }
            File.Copy(Sinfo.Path.Config + Sinfo.File.RWG, Sinfo.BackPath + Sinfo.File.RWG);
        }

        /// <summary>
        /// 恢复所有备份的文件
        /// </summary>
        public static void RestoreALL()
        {
            //Localization.txt与Localization - Quest.txt
            File.Delete(Sinfo.Path.Lang + Sinfo.File.Localization);
            File.Copy(Sinfo.BackPath + Sinfo.File.Localization, Sinfo.Path.Lang + Sinfo.File.Localization);

            File.Delete(Sinfo.Path.Lang + Sinfo.File.LocalizationQuest);
            File.Copy(Sinfo.BackPath + Sinfo.File.LocalizationQuest, Sinfo.Path.Lang + Sinfo.File.LocalizationQuest);

            //Assembly-CSharp.dll
            File.Delete(Sinfo.Path.Dll + Sinfo.File.GameDllAssembly);
            File.Copy(Sinfo.BackPath + Sinfo.File.GameDllAssembly, Sinfo.Path.Dll + Sinfo.File.GameDllAssembly);

            //xui.xml
            File.Delete(Sinfo.Path.Config + Sinfo.File.Xui);
            File.Copy(Sinfo.BackPath + Sinfo.File.Xui, Sinfo.Path.Config + Sinfo.File.Xui);

            //sytles.xml
            File.Delete(Sinfo.Path.XUi + Sinfo.File.XuiStyle);
            File.Copy(Sinfo.BackPath + Sinfo.File.XuiStyle, Sinfo.Path.XUi + Sinfo.File.XuiStyle);

            //windows.xml
            File.Delete(Sinfo.Path.XUi + Sinfo.File.XuiWindows);
            File.Copy(Sinfo.BackPath + Sinfo.File.XuiWindows, Sinfo.Path.XUi + Sinfo.File.XuiWindows);

            //items.xml
            File.Delete(Sinfo.Path.Config + Sinfo.File.Items);
            File.Copy(Sinfo.BackPath + Sinfo.File.Items, Sinfo.Path.Config + Sinfo.File.Items);

            //blocks.xml
            File.Delete(Sinfo.Path.Config + Sinfo.File.Blocks);
            File.Copy(Sinfo.BackPath + Sinfo.File.Blocks, Sinfo.Path.Config + Sinfo.File.Blocks);

            //rwgmixer.xml
            File.Delete(Sinfo.Path.Config + Sinfo.File.RWG);
            File.Copy(Sinfo.BackPath + Sinfo.File.RWG, Sinfo.Path.Config + Sinfo.File.RWG);
        }
    }

}
